package com.sg.business.permission.ui.column;

import java.util.List;

import org.eclipse.jface.viewers.TableViewer;

import com.mobnut.commons.util.Utils;
import com.sg.business.permission.model.ActionDefinition;
import com.sg.business.permission.model.PermissionAction;

public class PassDownColumn extends AbstractCheckColumn {

	@Override
	protected boolean isTrue(PermissionAction action) {
		return Boolean.TRUE.equals(action.getPassDown());
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void setValue(TableViewer viewer, PermissionAction thisAction,
			boolean value) {
		thisAction.setPassDown(value);
		ActionDefinition actionDefinition = thisAction.getActionDefinition();
		String[] codes = actionDefinition.getCascadePassDownActionCodes();

		List<PermissionAction> permissionActions = (List<PermissionAction>) viewer
				.getInput();
		if (codes != null) {
			for (int i = 0; i < permissionActions.size(); i++) {
				PermissionAction otherAction = permissionActions.get(i);
				if (otherAction == thisAction) {
					continue;
				}
				String otherCode = otherAction.getActionDefinition().getCode();
				if (Utils.inArray(otherCode, codes)) {
					otherAction.setPassDown(thisAction.getPassDown());
				}
			}
		}
		viewer.refresh();
	}

}
